package com.shj.em.biz.sell.controller;

import com.shj.em.biz.sell.entity.QuotItem;
import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.sys.service.CdService;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.shj.em.common.annotation.Log;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;

import com.shj.em.biz.sell.entity.OrderItem;
import com.shj.em.biz.sell.service.OrderItemService;
import com.baomidou.mybatisplus.plugins.Page;
import lombok.extern.slf4j.Slf4j;
import com.shj.em.base.controller.BaseController;


/**
 * <p>
 * 订单产品信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-09
 */
@RestController
@Slf4j
@RequestMapping("/sell/orderItem")
public class OrderItemController extends BaseController<OrderItem>{

    @Autowired
    OrderItemService orderitemService;

    @Autowired
    CdService cdService;

    @Log("创建订单产品信息")
    @PostMapping
    public ResMsg add(@RequestBody OrderItem orderitem){
        orderitemService.insert(orderitem);
        return ResMsg.ok();
    }

    @Log("更新订单产品信息")
    @PutMapping
    public ResMsg edit(@RequestBody OrderItem orderitem){
        orderitemService.updateById(orderitem);
        return ResMsg.ok();
    }

    @GetMapping("/findOrderItemsByOrderId/{quotId}")
    public ResMsg findQuotItemsByQuotId(@PathVariable long quotId){
        List<OrderItem> items = orderitemService.getItemsByOrderId(quotId);
        return ResMsg.ok(items);
    }

    @Log("批量删除订单产品")
    @DeleteMapping( "/batchRemove/{ids}")
    public ResMsg batchRemove(@PathVariable("ids") String ids){
        orderitemService.deleteBatchIds(Arrays.asList(ids.split(",")));
        return ResMsg.ok();
    }




    private String prefix = "order/orderitem";

    @GetMapping("/gotoAdd/{orderId}")
	String gotoAdd(@PathVariable("orderId") Long orderId, Model model){
        OrderItem orderitem = new OrderItem();
        orderitem.setOrderId(orderId);
        model.addAttribute("isEdit", false);
        model.addAttribute("orderitem", orderitem);
        addOtherInfo(model);
	    return prefix + "/add";
	}

	@GetMapping("/gotoEdit/{id}")
	String gotoEdit(@PathVariable("id") Long id, Model model){
        OrderItem orderitem = orderitemService.selectById(id);
		model.addAttribute("orderitem", orderitem);
        model.addAttribute("isEdit", true);
        addOtherInfo(model);
	    return prefix + "/add";
	}

    private void addOtherInfo(Model model){
        List<Cd> units = cdService.queryCdsByType(CdService.CD_TYPE_UNIT);
        model.addAttribute("units", units);
    }




    @Log("删除订单产品")
	@PostMapping( "/remove")
	@ResponseBody
	public ResMsg remove(@RequestParam("id") Long id){
        try{
            orderitemService.deleteById(id);
            return ResMsg.ok();
        }catch(Exception e){
            return ResMsg.error();
        }
	}



}

